﻿using System.Collections.Generic;
using System.Web.Mvc;
using Calendar.Logic.Log;

namespace Calendar.Web.Core
{
    public class HandleExceptionAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            LogManager.Log(DsLogLevel.ERROR, "Ошибка при отработке контролера", "Текст ошибки: {0}", filterContext.Exception.Message);
        }
    }

    public class WriteLogAttribute : ActionFilterAttribute
    {
        private LogEvent _logEvent;
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.LogMessages = new List<string>();
            _logEvent = new LogEvent(x => filterContext.Controller.ViewBag.LogMessages.Add(x.Msg));
            _logEvent.start();
            LogManager.AddLoggerForAllLevel(_logEvent);

            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            _logEvent.shutdown();
            LogManager.RemoveLogger(_logEvent);

            base.OnActionExecuted(filterContext);
        }
    }
}